
#include "config.h"

#ifdef __WM_WIN32__

#include "graphics.h"

using namespace WtfGraphics;


Screen::Screen() {
	mInstance = GetModuleHandle(NULL);

	mWndClass.cbSize = sizeof(WNDCLASSEX);
	mWndClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;	// Redraw On Size, And Own DC For Window.
	mWndClass.lpfnWndProc = (WNDPROC) &Screen::WindowProc;
	mWndClass.cbClsExtra = 0;									// No Extra Window Data
	mWndClass.cbWndExtra = 0;									// No Extra Window Data
	mWndClass.hInstance = mInstance;				// Set The Instance
	mWndClass.hIcon = NULL;
	mWndClass.hIconSm = NULL;
	mWndClass.hCursor = LoadCursor(NULL, IDC_ARROW);			// Load The Arrow Pointer
	mWndClass.hbrBackground	= NULL;
	mWndClass.lpszMenuName = NULL;									// We Don't Want A Menu
	mWndClass.lpszClassName	= "WtfEngine";
	

	if (RegisterClassEx(&mWndClass) == 0) {
		throw std::runtime_error("Win32: failed to register window class");
	}
};

void Screen::SetMode(const GraphicsMode& mode)
{
	RECT		WindowRect;
	WindowRect.left=(long)0;
	WindowRect.right=(long)mode.Resolution.x();
	WindowRect.top=(long)0;
	WindowRect.bottom=(long)mode.Resolution.y();

	switch (mode.WindowStyle) {
	case GraphicsMode::FULLSCREEN:
		mWndExStyle=WS_EX_APPWINDOW;
		mWndStyle=WS_POPUP;
		break;
	case GraphicsMode::BORDERLESS:
		mWndExStyle=WS_EX_APPWINDOW;
		mWndStyle=WS_OVERLAPPEDWINDOW;
		break;
	case GraphicsMode::FIXED:
		mWndExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
		mWndStyle=WS_CAPTION | WS_SYSMENU | WS_BORDER | WS_OVERLAPPED | WS_MINIMIZEBOX;
		break;
	default:
		mWndExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
		mWndStyle=WS_OVERLAPPEDWINDOW;
	};

	// Adjust Window To True Requested Size
	if(!AdjustWindowRectEx(&WindowRect, (mWndStyle & ~WS_OVERLAPPED), FALSE, mWndExStyle)) {
		throw std::runtime_error("Win32: Failed to adjust window size");
	};

	// Create The Window
	if (!(mHWnd = CreateWindowEx(mWndExStyle,							// Extended Style For The Window
								"WtfEngine",						// Class Name
								mode.sWindowTitle.c_str(),			// Window Title
								mWndStyle,
								0, 0,								// Window Position
								WindowRect.right-WindowRect.left,	// Calculate Window Width
								WindowRect.bottom-WindowRect.top,	// Calculate Window Height
								GetDesktopWindow(),
								NULL,								// No Menu
								mInstance,
								NULL)))								// Dont Pass Anything To WM_CREATE
	{
		DWORD err = GetLastError();
		throw std::runtime_error("Win32: Failed to create window");
	}

	ShowWindow(mHWnd, SW_SHOW);
	SetForegroundWindow(mHWnd);
	SetFocus(mHWnd);
};

void Screen::Activate() {
};

void Screen::Deactivate() {
};

LRESULT CALLBACK Screen::WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	// TODO: handle resize events
	return DefWindowProc(hWnd, msg, wParam, lParam);
};

GraphicsMode::GraphicsMode()
: sWindowTitle("WTF Engine"), bHwAccel(true), WindowStyle(GraphicsMode::RESIZABLE),
  Resolution(iVector2D(1024, 768)), uBitsPerPixel(32), uNumBuffers(2)
{
};

#endif
